package com.cx.bank.controller;

import com.cx.bank.mapper.UserMapper;
import com.cx.bank.pojo.UserBean;
import com.cx.bank.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class RegisterController {
    @Autowired
    UserMapper userMapper;
    @Autowired
    RegisterService registerService;
    //跳转注册页面
    @RequestMapping("/register")
    public String register(Model model,HttpSession session){
        return "registerPage";
    }
    //执行注册操作
    @RequestMapping("/doRegister")
    public String doRegister(@RequestParam("userName")String userName
            , @RequestParam("password")String password, @RequestParam("identity")int identity, Model model, HttpSession session
                             ){
        if (userName.equals("")) {
            model.addAttribute("msg", "请输入用户名");
            return "/registerPage";
        }
        if (password.equals("")){
            model.addAttribute("msg","请输入密码");
            return "/registerPage";
        }

        boolean ifRegist = registerService.register(userName,password,identity);
        userMapper.findUserByName(userName);
        if (ifRegist){
            return "redirect:/doRegisterSuccess";
        } else {
            model.addAttribute("msg", "用户已注册");
            return "registerPage";
        }
    }
}
